pacman::p_load(sf, terra, spatstat,
tmap, rvest, tidyverse)Hands-on Exercise 2
Overview
Spatial Point Pattern Analysis (SPPA) is the evaluation of the pattern or distribution of a set of points on a surface. The points may represent:
- Events such as crimes, traffic accidents, or disease onsets, or
- Business services (e.g., coffee shops and fast-food outlets) or facilities such as childcare centres and eldercare centres.
First-order Spatial Point Pattern Analysis (1st-SPPA) focuses on understanding the intensity or density of points across a study area. It examines how the distribution of points varies over space, essentially identifying trends or patterns in point density. This type of analysis deals with the individual locations of points and their distribution, without considering interactions between them.
Second-order spatial point pattern analysis examines the relationships between points, studying whether they are clustered, dispersed, or randomly distributed using tools like Ripley’s K function or nearest-neighbor analysis. It answers the question: “How do points interact with each other in space?”
1 First Order Spatial Point Pattern Analysis
In essence, 1st-SPPA helps answer questions such as:
- Where are points most densely located within the study area?
- Is point density uniform, or does it vary across space?
- How spread out is the point pattern?
In this chapter, we will gain hands-on experience with spatstat to perform two commonly used 1st-SPPA methods.
The specific questions we would like to answer are as follows:
- Are the childcare centres in Singapore randomly distributed throughout the country?
- If the answer is not, then the next logical question is: where are the locations with higher concentration of childcare centres?
1.1 The Data
Child Care Services data from data.gov.sg, a point feature data providing both location and attribute information of childcare centres. Master Plan 2019 Subzone Boundary (No Sea), a polygon feature data providing information of URA 2019 Master Plan Planning Subzone boundary data.
geojson format were used
1.2 Installing and Loading the R packages
In this hands-on exercise, we will use five R packages:
- sf: A modern package designed to import, manage, and process vector-based geospatial data in R.
- spatstat: A powerful package for point pattern analysis. We will use it to perform 1st- and 2nd-order spatial point pattern analysis and derive kernel density estimation (KDE) layers.
- terra: A fast and efficient package for working with raster and vector data, designed to replace the older raster package. In our exercise, we will use it to convert image outputs generated by
spatstatinto terra format.
- tmap: A package for creating cartographic-quality static maps and interactive maps (via the leaflet API).
- rvest: A package for web scraping, which we can use to harvest data directly from web pages.
We will install and load these packages using the following code:
1.3 Importing and Wrangling Geospatial Data Sets
subzone_path <- "data/geospatial/MasterPlan2019SubzoneBoundaryNoSeaGEOJSON.geojson"
childcare_path <- "data/geospatial/ChildCareServices.geojson"# Read datasets
mpsz_sf <- st_read(subzone_path, quiet = TRUE)
childcare_sf <- st_read(childcare_path, quiet = TRUE)
# Quick checks
print(st_geometry_type(mpsz_sf)[1])[1] MULTIPOLYGON
18 Levels: GEOMETRY POINT LINESTRING POLYGON MULTIPOINT ... TRIANGLE
print(st_geometry_type(childcare_sf)[1])[1] POINT
18 Levels: GEOMETRY POINT LINESTRING POLYGON MULTIPOINT ... TRIANGLE
#Validate geometry
mpsz_sf <- st_make_valid(mpsz_sf)
childcare_sf <- st_make_valid(childcare_sf)#Transform both datasets to Singapore SVY21 CRS (EPSG:3414)
target_epsg <- 3414
mpsz_sf <- st_transform(mpsz_sf, target_epsg)
childcare_sf <- st_transform(childcare_sf, target_epsg)#Confirm both are aligned
st_crs(mpsz_sf)$epsg[1] 3414
st_crs(childcare_sf)$epsg[1] 3414
identical(st_crs(mpsz_sf), st_crs(childcare_sf))[1] TRUE
# Should return TRUE#Optional tidying (only if these fields exist in subzone data)
if (all(c("SUBZONE_N","PLN_AREA_N") %in% names(mpsz_sf))) {
mpsz_sf <- mpsz_sf |>
filter(SUBZONE_N != "SOUTHERN GROUP",
PLN_AREA_N != "WESTERN ISLANDS",
PLN_AREA_N != "NORTH-EASTERN ISLANDS")
}1.3.1 Mapping the geo spatial data sets
#Quick visual check
tmap_mode("plot")
tm_shape(mpsz_sf) + tm_polygons(alpha = 0.2) +
tm_shape(childcare_sf) + tm_dots(size = 0.03, col = "red")
Interactive point symbol map
tmap_mode('view')
tm_shape(childcare_sf)+
tm_dots()tmap_mode('plot')1.4 Geospatial Data Wrangling
spatstat relies on its own specific data structures like ppp (planar point pattern) for point data and owin for observation windows. In this section, we will learn how to convert sf (Simple Features) objects into spatstat ppp and owin object.
1.4.1 Converting sf data frames to ppp class
The spatstat package requires point event data in ppp object form.
We can use the [as.ppp()] function from spatstat to convert childcare_sf into ppp format. class() of Base R will be used to verify the object class of childcare_ppp.
childcare_ppp <- as.ppp(childcare_sf)
class(childcare_ppp)[1] "ppp"
Summary statistics
summary(childcare_ppp)Marked planar point pattern: 1925 points
Average intensity 2.417322e-06 points per square unit
Coordinates are given to 11 decimal places
Mark variables: Name, Description
Summary:
Name Description
Length:1925 Length:1925
Class :character Class :character
Mode :character Mode :character
Window: rectangle = [11810.03, 45404.25] x [25596.33, 49300.88] units
(33590 x 23700 units)
Window area = 796336000 square units
1.4.2 Creating owin object
When analysing spatial point patterns, it is a good practice to confine the analysis with a geographical area like Singapore boundary. In spatstat, an object called owin is specially designed to represent this polygonal region. The code chunk below, as.owin() of spatstat is used to covert mpsz_sf into owin object of spatstat.
sg_owin <- as.owin(mpsz_sf)
class(sg_owin)[1] "owin"
plot(sg_owin)
1.4.3 Combining point events object and owin object
In this last step of geospatial data wrangling, we will extract childcare events that are located within Singapore by using the code chunk below.
childcareSG_ppp = childcare_ppp[sg_owin]childcareSG_pppMarked planar point pattern: 1925 points
Mark variables: Name, Description
window: polygonal boundary
enclosing rectangle: [2667.54, 56396.44] x [15748.72, 50256.33] units
1.5 Clark-Evan Test for Nearest Neighbour Analysis
Nearest Neighbor Analysis (NNA) is a spatial statistics method that calculates the average distance between each point and its closest neighbor to determine if a pattern of points is clustered, dispersed, or randomly distributed.
Clark-Evans test is a specific statistical method used within NNA to quantify whether a point pattern is clustered, random, or uniformly spaced, using the Clark-Evans aggregation index (R) to describe this pattern. NNA provides a numerical value that describes the degree of clustering or regularity, and the Clark-Evans test calculates a specific index (R) for this purpose
In this section, we will perform the Clark-Evans test of aggregation for a spatial point pattern by using [clarkevans.test()] from the spatstat.explore package.
The test hypotheses are:
- H₀: The distribution of childcare services is randomly distributed.
- H₁: The distribution of childcare services is not randomly distributed.
We will use a 95% confidence interval for this test.
1.5.1 Perform the Clark-Evans test without CSR
clarkevans.test() of spatstat.explore package support two Clark-Evans test, namely: without CRS and with CRS. In the code chunk below, Clark-Evans test without CSR method is used.
clarkevans.test(childcareSG_ppp,
correction="none",
clipregion="sg_owin",
alternative=c("clustered"))
Clark-Evans test
No edge correction
Z-test
data: childcareSG_ppp
R = 0.49444, p-value < 2.2e-16
alternative hypothesis: clustered (R < 1)
Statistical Conclusion:
From the Clark-Evans test, the observed R value is 0.53532 with a p-value < 2.2e-16.
Since the p-value is far below 0.05, we reject the null hypothesis of complete spatial randomness.
The result supports the alternative hypothesis that childcare services are clustered rather than randomly distributed.
Business Communication:
Based on this analysis, we conclude that childcare centres in Singapore tend to be located in clusters.
This clustering likely reflects demand-driven planning (e.g., centres opening in areas with higher child populations) or zoning policies.
For decision-makers, this indicates that underserved areas may exist where childcare services are sparse, highlighting opportunities for policy adjustments or business expansion into less-served neighborhoods.
1.6 Kernel Density Estimation Method
Kernel Density Estimation (KDE) is a valuable tool for visualising and analyzing first-order spatial point patterns. It is widely considered a method within Exploratory Spatial Data Analysis (ESDA) because it’s used to visualize and understand spatial data patterns by transforms discrete point data (like locations of childcare service, crime incidents or disease cases) into continuous density surfaces that reveal clusters and variations in event occurrences, without making prior assumptions about data distribution. It helps to begin understanding data distribution, identify hotspots, and explore relationships between spatial variables before performing more rigorous analysis.
In this section, you will learn how to compute the kernel density estimation (KDE) of childcare services in Singapore.
1.6.1 Working with automatic bandwidth selection method
The code chunk below computes a kernel density by using the following configurations of [density()] from spatstat:
bw.diggle(): automatic bandwidth selection method.
Other recommended methods are [bw.CvL()], [bw.scott()], or [bw.ppl()].
- The smoothing kernel used is gaussian (default).
Other smoothing methods include:"epanechnikov","quartic", or"disc".
- The intensity estimate can be corrected for edge effect bias using the method described by Jones (1993) and Diggle (2010, equation 18.9).
The default is FALSE.
kde_SG_diggle <- density(
childcareSG_ppp,
sigma=bw.diggle,
edge=TRUE,
kernel="gaussian")
plot(kde_SG_diggle)
#summary(kde_SG_diggle)summary(kde_SG_diggle)real-valued pixel image
128 x 128 pixel array (ny, nx)
enclosing rectangle: [2667.537, 56396.44] x [15748.72, 50256.33] units
dimensions of each pixel: 420 x 269.5907 units
Image is defined on a subset of the rectangular grid
Subset area = 784443186.143552 square units
Subset area fraction = 0.423
Pixel values (inside window):
range = [-1.007235e-20, 3.727444e-05]
integral = 1927.013
mean = 2.456536e-06
bw <- bw.diggle(childcareSG_ppp)
bw sigma
295.4419
1.6.2 Rescalling KDE values
In the code chunk below, rescale.ppp() is used to covert the unit of measurement from meter to kilometer.
childcareSG_ppp_km <- rescale.ppp(
childcareSG_ppp, 1000, "km")Now, we can re-run density() using the resale data set and plot the output kde map.
kde_childcareSG_km <- density(childcareSG_ppp_km,
sigma=bw.diggle,
edge=TRUE,
kernel="gaussian")
plot(kde_childcareSG_km)
1.6.3 Working with different kernel methods
By default, the kernel method used in density.ppp() is gaussian. But there are three other options, namely: Epanechnikov, Quartic and Dics.
The code chunk below will be used to compute three more kernel density estimations by using these three kernel function.
# Arrange in 2x2 grid with larger maps
par(mfrow = c(2, 2), mar = c(2, 2, 3, 2)) # adjust margins
# Gaussian
plot(density(childcareSG_ppp_km,
sigma = 0.2959712,
edge = TRUE,
kernel = "gaussian"),
main = "")
title("Gaussian", line = 1)
# Epanechnikov
plot(density(childcareSG_ppp_km,
sigma = 0.2959712,
edge = TRUE,
kernel = "epanechnikov"),
main = "")
title("Epanechnikov", line = 1)
# Quartic
plot(density(childcareSG_ppp_km,
sigma = 0.2959712,
edge = TRUE,
kernel = "quartic"),
main = "")
title("Quartic", line = 1)
# Disc
plot(density(childcareSG_ppp_km,
sigma = 0.2959712,
edge = TRUE,
kernel = "disc"),
main = "")
title("Disc", line = 1)
1.7 Fixed and Adaptive KDE
1.7.1 Computing KDE by using fixed bandwidth
Next, we will compute a KDE layer by defining a bandwidth of 600 meter. Notice that in the code chunk below, the sigma value used is 0.6. This is because the unit of measurement of childcareSG_ppp_km object is in kilometer, hence the 600m is 0.6km.
kde_childcareSG_fb <- density(childcareSG_ppp_km,
sigma=0.6,
edge=TRUE,
kernel="gaussian")
plot(kde_childcareSG_fb)
kde_childcareSG_ab <- adaptive.density(childcareSG_ppp_km,
sigma = 0.6,
edge = TRUE)
par(mar = c(4, 4, 6, 2)) # more space at top
plot(kde_childcareSG_ab, main = "Kernel Density of Childcare Services")
We can compare the fixed and adaptive kernel density estimation outputs by using the code chunk below.
par(mfrow=c(1,2))
plot(kde_childcareSG_fb, main = "Fixed bandwidth")
plot(kde_childcareSG_ab, main = "Adaptive bandwidth")
1.7.2 Computing KDE by using adaptive bandwidth
Fixed bandwidth method is very sensitive to highly skew distribution of spatial point patterns over geographical units for example urban versus rural. One way to overcome this problem is by using adaptive bandwidth instead.
kde_childcareSG_ab <- adaptive.density(
childcareSG_ppp_km,
method="kernel")
plot(kde_childcareSG_ab)
library(spatstat.geom)
summary(childcareSG_ppp_km) # look at 'window' units and rangesMarked planar point pattern: 1925 points
Average intensity 2.452904 points per square km
Coordinates are given to 14 decimal places
Mark variables: Name, Description
Summary:
Name Description
Length:1925 Length:1925
Class :character Class :character
Mode :character Mode :character
Window: polygonal boundary
82 separate polygons (36 holes)
vertices area relative.area
polygon 1 285 1.61128e+00 2.05e-03
polygon 2 719 5.40368e+01 6.89e-02
polygon 3 44 2.26583e-03 2.89e-06
polygon 4 27 1.50311e-02 1.92e-05
polygon 5 708 1.28815e+01 1.64e-02
polygon 6 (hole) 41 -4.01658e-02 -5.12e-05
polygon 7 (hole) 316 -5.11288e-02 -6.52e-05
polygon 8 77 3.29940e-01 4.20e-04
polygon 9 (hole) 3 -1.31629e-16 -1.68e-19
polygon 10 (hole) 3 -1.79054e-08 -2.28e-11
polygon 11 30 2.80002e-02 3.57e-05
polygon 12 (hole) 4 -2.50786e-07 -3.20e-10
polygon 13 (hole) 3 -8.52625e-08 -1.09e-10
polygon 14 (hole) 3 -3.13721e-08 -4.00e-11
polygon 15 (hole) 3 -5.61019e-08 -7.15e-11
polygon 16 (hole) 5 -1.43178e-07 -1.82e-10
polygon 17 71 8.18741e-03 1.04e-05
polygon 18 (hole) 38 -7.79903e-03 -9.94e-06
polygon 19 (hole) 3 -3.81487e-09 -4.86e-12
polygon 20 (hole) 3 -4.87319e-08 -6.21e-11
polygon 21 91 1.49663e-02 1.91e-05
polygon 22 (hole) 3 -4.25791e-08 -5.43e-11
polygon 23 (hole) 4 -3.47312e-08 -4.43e-11
polygon 24 3 1.96470e-07 2.50e-10
polygon 25 (hole) 270 -1.21366e-03 -1.55e-06
polygon 26 (hole) 19 -4.37731e-06 -5.58e-09
polygon 27 (hole) 35 -1.38430e-04 -1.76e-07
polygon 28 (hole) 23 -1.99689e-05 -2.54e-08
polygon 29 40 1.38603e-02 1.77e-05
polygon 30 (hole) 41 -6.00332e-03 -7.65e-06
polygon 31 (hole) 11 -8.36614e-05 -1.07e-07
polygon 32 (hole) 3 -3.69186e-09 -4.70e-12
polygon 33 45 2.51228e-03 3.20e-06
polygon 34 137 3.22285e-03 4.11e-06
polygon 35 147 3.10396e-03 3.96e-06
polygon 36 (hole) 4 -2.46124e-10 -3.14e-13
polygon 37 75 1.73525e-02 2.21e-05
polygon 38 83 5.28926e-03 6.74e-06
polygon 39 211 4.70522e-01 6.00e-04
polygon 40 106 3.04131e-03 3.88e-06
polygon 41 264 1.50631e+00 1.92e-03
polygon 42 71 5.63055e-03 7.17e-06
polygon 43 10 1.99759e-04 2.55e-07
polygon 44 (hole) 3 -2.73199e-08 -3.48e-11
polygon 45 483 2.06117e+00 2.63e-03
polygon 46 155 2.67503e-01 3.41e-04
polygon 47 1020 1.27781e+00 1.63e-03
polygon 48 (hole) 5 -5.18342e-07 -6.60e-10
polygon 49 (hole) 4 -7.19058e-07 -9.16e-10
polygon 50 (hole) 3 -2.53510e-08 -3.23e-11
polygon 51 (hole) 4 -7.56223e-08 -9.64e-11
polygon 52 (hole) 4 -1.23063e-10 -1.57e-13
polygon 53 65 8.42861e-02 1.07e-04
polygon 54 (hole) 3 -1.06327e-07 -1.35e-10
polygon 55 47 3.82089e-02 4.87e-05
polygon 56 6 4.50376e-04 5.74e-07
polygon 57 129 9.53762e-02 1.22e-04
polygon 58 4 2.69408e-04 3.43e-07
polygon 59 1045 4.44510e+00 5.66e-03
polygon 60 22 6.74620e-03 8.60e-06
polygon 61 59 3.43163e-02 4.37e-05
polygon 62 14 5.86528e-03 7.47e-06
polygon 63 94 5.96175e-02 7.60e-05
polygon 64 234 2.08755e+00 2.66e-03
polygon 65 10 4.90997e-04 6.26e-07
polygon 66 232 4.72887e-01 6.03e-04
polygon 67 (hole) 13 -3.92141e-04 -5.00e-07
polygon 68 15 4.03288e-02 5.14e-05
polygon 69 227 1.10308e+00 1.41e-03
polygon 70 10 6.60201e-03 8.41e-06
polygon 71 19 3.09219e-02 3.94e-05
polygon 72 145 9.61783e-01 1.23e-03
polygon 73 30 4.28953e-03 5.47e-06
polygon 74 37 1.29491e-02 1.65e-05
polygon 75 4 9.47128e-05 1.21e-07
polygon 76 (hole) 3 -3.50914e-08 -4.47e-11
polygon 77 (hole) 3 -1.29022e-07 -1.64e-10
polygon 78 (hole) 4 -4.34886e-07 -5.54e-10
polygon 79 (hole) 4 -6.40504e-07 -8.16e-10
polygon 80 (hole) 3 -9.35842e-08 -1.19e-10
polygon 81 15268 7.00834e+02 8.93e-01
polygon 82 (hole) 3 -1.80743e-08 -2.30e-11
enclosing rectangle: [2.66754, 56.39644] x [15.74872, 50.25633] km
(53.73 x 34.51 km)
Window area = 784.784 square km
Unit of length: 1 km
Fraction of frame area: 0.423
unitname(childcareSG_ppp_km) # should say "kilometre"km / km
1.8 Plotting cartographic quality KDE map
1.8.1 Converting gridded output into raster
Next, we will convert the im kernal density objects into SpatRaster object by using rast() of terra package.
kde_childcareSG_bw_terra <- rast(kde_childcareSG_km)
class(kde_childcareSG_bw_terra)[1] "SpatRaster"
attr(,"package")
[1] "terra"
kde_childcareSG_bw_terraclass : SpatRaster
size : 128, 128, 1 (nrow, ncol, nlyr)
resolution : 0.4197571, 0.2695907 (x, y)
extent : 2.667537, 56.39644, 15.74872, 50.25633 (xmin, xmax, ymin, ymax)
coord. ref. :
source(s) : memory
name : lyr.1
min value : -1.319098e-14
max value : 3.727444e+01
unit : km
Notice that the crs property is empty. #### Assigning projection systems In code chunk below, crs() of terra is used to assign the CRS information on kde_childcareSG_bw_terra layer.
crs(kde_childcareSG_bw_terra) <- "EPSG:3414"
kde_childcareSG_bw_terraclass : SpatRaster
size : 128, 128, 1 (nrow, ncol, nlyr)
resolution : 0.4197571, 0.2695907 (x, y)
extent : 2.667537, 56.39644, 15.74872, 50.25633 (xmin, xmax, ymin, ymax)
coord. ref. : SVY21 / Singapore TM (EPSG:3414)
source(s) : memory
name : lyr.1
min value : -1.319098e-14
max value : 3.727444e+01
unit : km
1.8.2 Plotting KDE map with tmap
Finally, we will display the raster in cartographic quality map using tmap package.
tm_shape(kde_childcareSG_bw_terra) +
tm_raster(col.scale =
tm_scale_continuous(
values = "viridis"),
col.legend = tm_legend(
title = "Density values",
title.size = 0.7,
text.size = 0.7,
bg.color = "white",
bg.alpha = 0.7,
position = tm_pos_in(
"right", "bottom"),
frame = TRUE)) +
tm_graticules(labels.size = 0.7) +
tm_compass() +
tm_layout(scale = 1.0)
1.9 First Order SPPA at the Planning Subzone Level
In this section, we would like to further our analysis at the planning area level. For simplicity reason, we will focus on Punggol, Tampines Chua Chu Kand and Jurong West planning areas
1.9.1 Geospatial data wrangling
extract_kml_field <- function(html_text, field_name) {
if (is.na(html_text) || html_text == "") return(NA_character_)
page <- read_html(html_text)
rows <- page %>% html_elements("tr")
value <- rows %>%
keep(~ html_text2(html_element(.x, "th")) == field_name) %>%
html_element("td") %>%
html_text2()
if (length(value) == 0) NA_character_ else value
}library(purrr)
library(rvest)
mpsz_sf <- mpsz_sf %>%
mutate(
REGION_N = map_chr(Description, extract_kml_field, "REGION_N"),
PLN_AREA_N = map_chr(Description, extract_kml_field, "PLN_AREA_N"),
SUBZONE_N = map_chr(Description, extract_kml_field, "SUBZONE_N"),
SUBZONE_C = map_chr(Description, extract_kml_field, "SUBZONE_C")
)1.9.1.1 Extracting study area
# Extract target planning areas
pg <- mpsz_sf %>%
filter(PLN_AREA_N == "PUNGGOL")
tm <- mpsz_sf%>%
filter(PLN_AREA_N == "TAMPINES")
ck <- mpsz_sf %>%
filter(PLN_AREA_N == "CHOA CHU KANG")
jw <- mpsz_sf %>%
filter(PLN_AREA_N == "JURONG WEST")It is always a good practice to review the extracted areas. The code chunk below will be used to plot the extracted planning areas.
par(mfrow=c(2,2))
plot(st_geometry(pg), main = "Ponggol")
plot(st_geometry(tm), main = "Tampines")
plot(st_geometry(ck), main = "Choa Chu Kang")
plot(st_geometry(jw), main = "Jurong West")
1.9.1.2 Creating owin object
Now, we will convert these sf objects into owin objects that is required by spatstat.
pg_owin = as.owin(pg)
tm_owin = as.owin(tm)
ck_owin = as.owin(ck)
jw_owin = as.owin(jw)1.9.1.3 Combining point events object and owin object
childcare_pg_ppp = childcare_ppp[pg_owin]
childcare_tm_ppp = childcare_ppp[tm_owin]
childcare_ck_ppp = childcare_ppp[ck_owin]
childcare_jw_ppp = childcare_ppp[jw_owin]Next, rescale.ppp() function is used to trasnform the unit of measurement from metre to kilometre.
childcare_pg_ppp.km = rescale.ppp(childcare_pg_ppp, 1000, "km")
childcare_tm_ppp.km = rescale.ppp(childcare_tm_ppp, 1000, "km")
childcare_ck_ppp.km = rescale.ppp(childcare_ck_ppp, 1000, "km")
childcare_jw_ppp.km = rescale.ppp(childcare_jw_ppp, 1000, "km")The code chunk below is used to plot these four study areas and the locations of the childcare centres.
par(mfrow=c(2,2))
plot(unmark(childcare_pg_ppp.km),
main="Punggol", pch=20, cex=0.6)
plot(unmark(childcare_tm_ppp.km),
main="Tampines", pch=20, cex=0.6)
plot(unmark(childcare_ck_ppp.km),
main="Choa Chu Kang", pch=20, cex=0.6)
plot(unmark(childcare_jw_ppp.km),
main="Jurong West", pch=20, cex=0.6)
1.9.2 Clark and Evans Test
1.9.2.1 Choa Chu Kang planning area
In the code chunk below, clarkevans.test() of spatstat is used to performs Clark-Evans test of aggregation for childcare centre in Choa Chu Kang planning area.
clarkevans.test(childcare_ck_ppp,
correction="none",
clipregion=NULL,
alternative=c("two.sided"),
nsim=999)
Clark-Evans test
No edge correction
Z-test
data: childcare_ck_ppp
R = 0.84097, p-value = 0.008865
alternative hypothesis: two-sided
1.9.2.2 Tampines planning area
In the code chunk below, the similar test is used to analyse the spatial point patterns of childcare centre in Tampines planning area.
clarkevans.test(childcare_tm_ppp,
correction="none",
clipregion=NULL,
alternative=c("two.sided"),
nsim=999)
Clark-Evans test
No edge correction
Z-test
data: childcare_tm_ppp
R = 0.66817, p-value = 6.579e-12
alternative hypothesis: two-sided
1.9.3 Computing KDE surfaces by planning area
The code chunk below will be used to compute the KDE of these four planning area. bw.diggle method is used to derive the bandwidth of each
par(mfrow=c(2,2))
# Punggol
plot(density(childcare_pg_ppp.km,
sigma = bw.diggle(childcare_pg_ppp.km),
edge = TRUE, kernel="gaussian"),
main = "Punggol")
# Tampines
plot(density(childcare_tm_ppp.km,
sigma = bw.diggle(childcare_tm_ppp.km),
edge = TRUE, kernel="gaussian"),
main = "Tampines")
# Choa Chu Kang
plot(density(childcare_ck_ppp.km,
sigma = bw.diggle(childcare_ck_ppp.km),
edge = TRUE, kernel="gaussian"),
main = "Choa Chu Kang")
# Jurong West
plot(density(childcare_jw_ppp.km,
sigma = bw.diggle(childcare_jw_ppp.km),
edge = TRUE, kernel="gaussian"),
main = "Jurong West")
##2nd Order Spatial Point Patterns Analysis Methods Second-order spatial point pattern analysis examines the spatial relationships between points in a pattern, specifically focusing on how the presence of one point influences the location of others. It goes beyond simply describing the overall density of points (first-order effects) by investigating clustering, dispersion, or randomness at various spatial scales.
Using appropriate functions of spatstat, this hands-on exercise aims to discover the spatial point processes of childcare centres in Singapore.
The specific questions we would like to answer are as follows:
- Are the childcare centres in Singapore randomly distributed throughout the country?
- If the answer is not, then the next logical question is: where are the locations with higher concentration of childcare centres? ### Data
- Child Care Services data from data.gov.sg, a point feature data providing both location and attribute information of childcare centres.
- Master Plan 2019 Subzone Boundary (No Sea), a polygon feature data providing information of URA 2019 Master Plan Planning Subzone boundary data. ### Data Check
par(mfrow=c(2,2))
plot(unmark(childcare_pg_ppp.km),
main="Punggol", pch=20, cex=0.6)
plot(unmark(childcare_tm_ppp.km),
main="Tampines", pch=20, cex=0.6)
plot(unmark(childcare_ck_ppp.km),
main="Choa Chu Kang", pch=20, cex=0.6)
plot(unmark(childcare_jw_ppp.km),
main="Jurong West", pch=20, cex=0.6)
1.10 Analysing Spatial Point Process Using G-Function
The G function measures the distribution of the distances from an arbitrary event to its nearest event. In this section, We will learn how to compute G-function estimation by using Gest() of spatstat package. We will also learn how to perform monta carlo simulation test using envelope() of spatstat package.
1.10.1 Choa Chu Kang planning area
1.10.1.1 Computing G-function estimation
The code chunk below is used to compute G-function using Gest() of spatat package.
set.seed(1234)
G_CK = Gest(childcare_ck_ppp, correction = "border")
plot(G_CK, xlim=c(0,500))
1.10.1.2 Performing Complete Spatial Randomness Test
To confirm the observed spatial patterns above, a hypothesis test will be conducted. The hypothesis and test are as follows:
Ho = The distribution of childcare services at Choa Chu Kang are randomly distributed.
H1= The distribution of childcare services at Choa Chu Kang are not randomly distributed.
The null hypothesis will be rejected if p-value is smaller than alpha value of 0.001.
Monte Carlo test with G-fucntion
G_CK.csr <- envelope(childcare_ck_ppp, Gest, nsim = 999)Generating 999 simulations of CSR ...
1, 2, 3, ......10.........20.........30.........40.........50.........60..
.......70.........80.........90.........100.........110.........120.........130
.........140.........150.........160.........170.........180.........190........
.200.........210.........220.........230.........240.........250.........260......
...270.........280.........290.........300.........310.........320.........330....
.....340.........350.........360.........370.........380.........390.........400..
.......410.........420.........430.........440.........450.........460.........470
.........480.........490.........500.........510.........520.........530........
.540.........550.........560.........570.........580.........590.........600......
...610.........620.........630.........640.........650.........660.........670....
.....680.........690.........700.........710.........720.........730.........740..
.......750.........760.........770.........780.........790.........800.........810
.........820.........830.........840.........850.........860.........870........
.880.........890.........900.........910.........920.........930.........940......
...950.........960.........970.........980.........990........
999.
Done.
plot(G_CK.csr)
1.10.2 Tampines planning area
1.10.2.1 Computing G-function estimation
G_tm = Gest(childcare_tm_ppp, correction = "best")
plot(G_tm)
1.10.2.2 Performing Complete Spatial Randomness Test
To confirm the observed spatial patterns above, a hypothesis test will be conducted. The hypothesis and test are as follows:
Ho = The distribution of childcare services at Tampines are randomly distributed.
H1= The distribution of childcare services at Tampines are not randomly distributed.
The null hypothesis will be rejected is p-value is smaller than alpha value of 0.001.
The code chunk below is used to perform the hypothesis testing.
G_tm.csr <- envelope(childcare_tm_ppp, Gest, correction = "all", nsim = 999)Generating 999 simulations of CSR ...
1, 2, 3, ......10.........20.........30.........40.........50.........60..
.......70.........80.........90.........100.........110.........120.........130
.........140.........150.........160.........170.........180.........190........
.200.........210.........220.........230.........240.........250.........260......
...270.........280.........290.........300.........310.........320.........330....
.....340.........350.........360.........370.........380.........390.........400..
.......410.........420.........430.........440.........450.........460.........470
.........480.........490.........500.........510.........520.........530........
.540.........550.........560.........570.........580.........590.........600......
...610.........620.........630.........640.........650.........660.........670....
.....680.........690.........700.........710.........720.........730.........740..
.......750.........760.........770.........780.........790.........800.........810
.........820.........830.........840.........850.........860.........870........
.880.........890.........900.........910.........920.........930.........940......
...950.........960.........970.........980.........990........
999.
Done.
plot(G_tm.csr)
1.11 Analysing Spatial Point Process Using F-Function
The F function estimates the empty space function F(r) or its hazard rate h(r) from a point pattern in a window of arbitrary shape. In this section, we will learn how to compute F-function estimation by using Fest() of spatstat package. we will also learn how to perform monta carlo simulation test using envelope() of spatstat package. #### Choa Chu Kang planning area ##### Computing F-function estimation The code chunk below is used to compute F-function using Fest() of spatat package.
F_CK = Fest(childcare_ck_ppp)
plot(F_CK)
1.11.0.1 Performing Complete Spatial Randomness Test
To confirm the observed spatial patterns above, a hypothesis test will be conducted. The hypothesis and test are as follows:
Ho = The distribution of childcare services at Choa Chu Kang are randomly distributed.
H1= The distribution of childcare services at Choa Chu Kang are not randomly distributed.
The null hypothesis will be rejected if p-value is smaller than alpha value of 0.001.
Monte Carlo test with F-fucntion
F_CK.csr <- envelope(childcare_ck_ppp, Fest, nsim = 999)Generating 999 simulations of CSR ...
1, 2, 3, ......10.........20.........30.........40.........50.........60..
.......70.........80.........90.........100.........110.........120.........130
.........140.........150.........160.........170.........180.........190........
.200.........210.........220.........230.........240.........250.........260......
...270.........280.........290.........300.........310.........320.........330....
.....340.........350.........360.........370.........380.........390.........400..
.......410.........420.........430.........440.........450.........460.........470
.........480.........490.........500.........510.........520.........530........
.540.........550.........560.........570.........580.........590.........600......
...610.........620.........630.........640.........650.........660.........670....
.....680.........690.........700.........710.........720.........730.........740..
.......750.........760.........770.........780.........790.........800.........810
.........820.........830.........840.........850.........860.........870........
.880.........890.........900.........910.........920.........930.........940......
...950.........960.........970.........980.........990........
999.
Done.
plot(F_CK.csr)
1.11.1 Tampines planning area
1.11.1.1 Computing F-function estimation
F_tm = Fest(childcare_tm_ppp, correction = "best")
plot(F_tm)
1.11.1.2 Performing Complete Spatial Randomness Test
To confirm the observed spatial patterns above, a hypothesis test will be conducted. The hypothesis and test are as follows:
Ho = The distribution of childcare services at Tampines are randomly distributed.
H1= The distribution of childcare services at Tampines are not randomly distributed.
The null hypothesis will be rejected is p-value is smaller than alpha value of 0.001.
The code chunk below is used to perform the hypothesis testing.
F_tm.csr <- envelope(childcare_tm_ppp, Fest, correction = "all", nsim = 999)Generating 999 simulations of CSR ...
1, 2, 3, ......10.........20.........30.........40.........50.........60..
.......70.........80.........90.........100.........110.........120.........130
.........140.........150.........160.........170.........180.........190........
.200.........210.........220.........230.........240.........250.........260......
...270.........280.........290.........300.........310.........320.........330....
.....340.........350.........360.........370.........380.........390.........400..
.......410.........420.........430.........440.........450.........460.........470
.........480.........490.........500.........510.........520.........530........
.540.........550.........560.........570.........580.........590.........600......
...610.........620.........630.........640.........650.........660.........670....
.....680.........690.........700.........710.........720.........730.........740..
.......750.........760.........770.........780.........790.........800.........810
.........820.........830.........840.........850.........860.........870........
.880.........890.........900.........910.........920.........930.........940......
...950.........960.........970.........980.........990........
999.
Done.
plot(F_tm.csr)
1.12 Analysing Spatial Point Process Using K-Function
K-function measures the number of events found up to a given distance of any particular event. In this section, We will learn how to compute K-function estimates by using Kest() of spatstat package. We will also learn how to perform monta carlo simulation test using envelope() of spatstat package.
1.12.1 5.8.1 Choa Chu Kang planning area
1.12.1.1 5.8.1.1 Computing K-fucntion estimate
K_ck = Kest(childcare_ck_ppp, correction = "Ripley")
plot(K_ck, . -r ~ r, ylab= "K(d)-r", xlab = "d(m)")
1.12.1.2 Performing Complete Spatial Randomness Test
To confirm the observed spatial patterns above, a hypothesis test will be conducted. The hypothesis and test are as follows:
Ho = The distribution of childcare services at Choa Chu Kang are randomly distributed.
H1= The distribution of childcare services at Choa Chu Kang are not randomly distributed.
The null hypothesis will be rejected if p-value is smaller than alpha value of 0.001.
The code chunk below is used to perform the hypothesis testing.
K_ck.csr <- envelope(childcare_ck_ppp, Kest, nsim = 99, rank = 1, glocal=TRUE)Generating 99 simulations of CSR ...
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
99.
Done.
plot(K_ck.csr, . - r ~ r, xlab="d", ylab="K(d)-r")
1.12.2 Tampines planning area
1.12.2.1 Computing K-fucntion estimation
K_tm = Kest(childcare_tm_ppp, correction = "Ripley")
plot(K_tm, . -r ~ r,
ylab= "K(d)-r", xlab = "d(m)",
xlim=c(0,1000))
1.12.2.2 Performing Complete Spatial Randomness Test
To confirm the observed spatial patterns above, a hypothesis test will be conducted. The hypothesis and test are as follows:
Ho = The distribution of childcare services at Tampines are randomly distributed.
H1= The distribution of childcare services at Tampines are not randomly distributed.
The null hypothesis will be rejected if p-value is smaller than alpha value of 0.001.
The code chunk below is used to perform the hypothesis testing.
K_tm.csr <- envelope(childcare_tm_ppp, Kest, nsim = 99, rank = 1, glocal=TRUE)Generating 99 simulations of CSR ...
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
99.
Done.
plot(K_tm.csr, . - r ~ r,
xlab="d", ylab="K(d)-r", xlim=c(0,500))
1.13 Analysing Spatial Point Process Using L-Function
In this section, we will learn how to compute L-function estimation by using Lest() of spatstat package. We will also learn how to perform monta carlo simulation test using envelope() of spatstat package.
1.13.1 Choa Chu Kang planning area
1.13.1.1 Computing L Fucntion estimation
L_ck = Lest(childcare_ck_ppp, correction = "Ripley")
plot(L_ck, . -r ~ r,
ylab= "L(d)-r", xlab = "d(m)")
1.13.1.2 Performing Complete Spatial Randomness Test
To confirm the observed spatial patterns above, a hypothesis test will be conducted. The hypothesis and test are as follows:
Ho = The distribution of childcare services at Choa Chu Kang are randomly distributed.
H1= The distribution of childcare services at Choa Chu Kang are not randomly distributed.
The null hypothesis will be rejected if p-value if smaller than alpha value of 0.001.
The code chunk below is used to perform the hypothesis testing.
L_ck.csr <- envelope(childcare_ck_ppp, Lest, nsim = 99, rank = 1, glocal=TRUE)Generating 99 simulations of CSR ...
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
99.
Done.
plot(L_ck.csr, . - r ~ r, xlab="d", ylab="L(d)-r")
1.13.2 Tampines planning area
1.13.2.1 Computing L-fucntion estimate
L_tm = Lest(childcare_tm_ppp, correction = "Ripley")
plot(L_tm, . -r ~ r,
ylab= "L(d)-r", xlab = "d(m)",
xlim=c(0,1000))
1.13.2.2 Performing Complete Spatial Randomness Test
To confirm the observed spatial patterns above, a hypothesis test will be conducted. The hypothesis and test are as follows:
Ho = The distribution of childcare services at Tampines are randomly distributed.
H1= The distribution of childcare services at Tampines are not randomly distributed.
The null hypothesis will be rejected if p-value is smaller than alpha value of 0.001.
The code chunk below will be used to perform the hypothesis testing.
L_tm.csr <- envelope(childcare_tm_ppp, Lest, nsim = 99, rank = 1, glocal=TRUE)Generating 99 simulations of CSR ...
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
99.
Done.
plot(L_tm.csr, . - r ~ r,
xlab="d", ylab="L(d)-r", xlim=c(0,500))